<?php
class LangController extends CommonController{
	public function Index(){
		$list = $this->db->table("@_lang")->order("id DESC")->findAll();
		include $this->display('lang.html');
	}

	public function Add(){
		$isPost		= isset($_GET['isPost']) ? intval($_GET['isPost']) : 0;
		if ($_POST) {
			$name			= getGpc('name', 'string', 'P');
			$intro 			= getGpc('intro', 'string', 'P');
			$lang_key 		= getGpc('lang_key', 'string', 'P');
			$domain			= getGpc('domain', 'string', 'P');
			if (empty($name)) {
				$this->msg('请填写名称。');
			}
			if (empty($lang_key)) {
				$this->msg('请填写主键');
			}
			
			$data	= array(
				'name'			=> $name,
				'intro'			=> $intro,
				'lang_key'		=> $lang_key,
				'domain'		=> $domain,
			);
			$this->db->table("@_lang")->add($data);
			$this->_cache();
			$result['status']	= 1;
			$result['msg']		= '保存成功';
			echo json_encode($result);
			exit();

		}
		ob_start();
		include $this->display('lang.html');
		$content = ob_get_contents() ;
		ob_end_clean() ;
		$result['status']	= 1;
		$result['content']	= $content;
		echo json_encode($result);
	}

	public function Edit(){
		$id 		= getGpc('id', 'integer', 'G');
		if (empty($id)) {
			$this->msg('参数错误。');
		}
		if ( isset($_POST) && $_POST ){
			$name			= getGpc('name', 'string', 'P');
			$intro 			= getGpc('intro', 'string', 'P');
			$lang_key 		= getGpc('lang_key', 'string', 'P');
			$domain			= getGpc('domain', 'string', 'P');
			if (empty($name)) {
				$this->msg('请填写名称。');
			}
			if (empty($lang_key)) {
				$this->msg('请填写主键');
			}
			
			$data	= array(
				'name'			=> $name,
				'intro'			=> $intro,
				'lang_key'		=> $lang_key,
				'domain'		=> $domain,
			);
			$this->db->table("@_lang")->where("id=$id")->update($data);
			$this->_cache();
			$result['status']	= 1;
			$result['msg']		= '修改成功';
			echo json_encode($result);
			exit();
		}
		$vo = $this->db->table("@_lang")->where("id=$id")->find();
		if (empty($vo)) {
			$this->msg('不存在此语言');
		}
		ob_start();
		include $this->display('lang.html');
		$content = ob_get_contents() ;
		ob_end_clean() ;
		$result['status']	= 1;
		$result['content']	= $content;
		echo json_encode($result);
	}

	private function _cache(){
		$list     = $this->db->table("@_lang")->order("id ASC")->findAll();
		$lang_item = array();
		foreach ($list as $key => $v) {
			$lang_item[$v['id']]	= $v['name'];
		}
		$this->cache->write($lang_item, 'lang_item');
	}
}
?>